//
//  EasyBaseNavController.m
//  TestDemo
//
//  Created by 张润东 on 2021/8/23.

#import "EasyBaseNavController.h"
@interface EasyBaseNavController ()

@end

@implementation EasyBaseNavController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self navDefaultSetting];
    // Do any additional setup after loading the view.
}

//想返回前处理请
-(void)backBtnClick{
    NSLog(@"点击回退按钮");
    [self popViewControllerAnimated:YES];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}

//默认样式，如果不实现的话。
-(void)navDefaultSetting{
    self.navigationBar.barStyle = UIBarStyleDefault;//状态栏黑色  UIBarStyleBlack:状态栏白色
    [self.navigationBar setShadowImage:[UIImage new]];//底部细线消失术
    [self.navigationBar setTranslucent:NO];//是否半透明标识
    [self.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor blackColor],NSFontAttributeName:[UIFont systemFontOfSize:19]}];//title文字颜色与大小
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self.navigationBar setBackgroundImage:[self imageWithColor:[UIColor whiteColor]] forBarMetrics:UIBarMetricsDefault];//背景颜色
}

-(void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{
    if (self.childViewControllers.count > 0) {
        viewController.hidesBottomBarWhenPushed = YES;
    }
    [super pushViewController:viewController animated:animated];
    //返回按钮设置
    if (self.childViewControllers.count>1) {
        viewController.navigationItem.leftBarButtonItem = [self backItem];
    }
}

-(UIButton*)customLeftBtn{
    UIButton *btn  = [self customBackButton];
    [btn addTarget:self action:@selector(backBtnClick) forControlEvents:UIControlEventTouchUpInside];
    return btn;
}

- (UIBarButtonItem *)backItem{
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:[self customLeftBtn]];
    return item;
}

-(UIButton*)customBackButton{
        UIButton * leftBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 50, 40)];
        UIImageView *leftArrow = [[UIImageView alloc] initWithFrame:CGRectZero];
        [leftBtn addSubview:leftArrow];
        NSString*imageName = @"nav_default_back";
        leftArrow.frame = CGRectMake(0, 0,20,20);
        UIImage*iamge = [[UIImage imageNamed:imageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        leftArrow.image = iamge;
        leftArrow.center=CGPointMake(leftArrow.frame.size.width/2,20);
       return leftBtn;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (UIImage*)imageWithColor:(UIColor *)color{
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, color.CGColor);
    CGContextFillRect(context, rect);
    UIImage* image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    NSData* imageData = UIImageJPEGRepresentation(image, 1.0f);
    image = [UIImage imageWithData:imageData];
    return image;
}

@end
